//
//  DaysHistogramView.m
//  MonitoringNetwork
//
//  Created by admin on 2018/1/4.
//  Copyright © 2018年 wtmcxlm. All rights reserved.
//

#import "DaysHistogramView.h"

@implementation DaysHistogramView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setUpUI];
    }
    return self;
}


- (void)setUpUI
{
    
}
- (void)loadDaysHistogramWithArr:(NSArray *)arr
{
    int maxNum = 0;
    int totalNum = 0;
    for (int i=0; i<arr.count; i++) {
        NSDictionary *dic = arr[i];
        totalNum += [dic[@"value"] intValue];
        if (maxNum<[dic[@"value"] intValue]) {
            maxNum = [dic[@"value"] intValue];
        }
    }
    
    int HorizontalSpacing = (kScreenWidth - 173.0f/3)/(arr.count-0.5);
    if (arr.count == 1) {
        HorizontalSpacing = 60;
    }else{
        
        if (HorizontalSpacing >60) {
            
        }else{
            HorizontalSpacing = 60;
        }
    }
    
    
    NSArray *colorArr = @[ColorOne,ColorTwo];
    int eachIntValu = maxNum/(4.0f);
    NSString *str  = [NSString stringWithFormat:@"%d",eachIntValu];
    int eachValu =  ceil(maxNum*1.0f/(4.0f)/pow(10,str.length-1))*pow(10,str.length-1);
    CGFloat lineVerticalLength = 261-143/3.0f - 188.0f/3;
    CGFloat verticalLength = lineVerticalLength/(4+1.0f);
    

    for (int i=0; i<arr.count; i++) {
        NSDictionary *dic = arr[i];
        int num = [dic[@"value"] intValue];
        CGFloat height = num*1.0f/eachValu*verticalLength;
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(HorizontalSpacing*i+30-20*0.5f, lineVerticalLength-height, 20.0f,height) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(8/3.f, 8/3.f)];
        CAShapeLayer *shaperLayer = [CAShapeLayer new];
        UIColor *color = colorArr[i%2];
        shaperLayer.fillColor = color.CGColor;
        [self.layer addSublayer:shaperLayer];
        shaperLayer.path = bezierPath.CGPath;
        UILabel *label = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:center andString:[NSString stringWithFormat:@"%@",dic[@"value"]]];
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_left).offset(HorizontalSpacing*i+30);
            make.bottom.mas_equalTo(self.mas_top).offset(lineVerticalLength-height);
        }];
    }
}
@end
